package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.model.SAS;
import com.bishe.psy.model.SCL90;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.ISasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-26
 * Time: 10:33
 */

@Slf4j
@Api(tags = "sas测试问卷接口")
@RestController
@RequestMapping("/sas")
public class SasController {

    @Resource
    private ISasService iSasService;

    @ApiOperation("新增一条sas测试数据")
    @GetMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "sas选项分值汇总") @RequestParam("data[]") String[] data) {

        //1.参数校验
        if(data.length <= 0) {
            return AppResult.failed("测试出错了");
        }

        //2.获取当前登录的用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //3.分值计算
        int anxiety_Score = 0;
        int coarse = 0;
        for(int i = 1; i < data.length; i++) {
            coarse = coarse + Integer.parseInt(data[i]);
        }

        anxiety_Score = (int) Math.floor(coarse * 1.25);

        System.out.println("粗粉结果 = " + coarse);
        System.out.println("相乘结果 = " + coarse * 1.25 );
        System.out.println("取整结果 = " + anxiety_Score );

        //4.创建scl90记录对象
        SAS sas = new SAS();
        sas.setUserId(stuUser.getId());
        sas.setAnxiety_Score(anxiety_Score);

        //4.调用service
        iSasService.create(sas);

        //5.返回结果
        return AppResult.success();
    }




    @ApiOperation("查询sas新增测评记录接口")
    @GetMapping("/select")
    public AppResult<SAS> selectById(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        SAS sas = iSasService.selectByIdNewResult(stuUser.getId());

        //3.判断查询结果是否为空
        if(sas == null || sas.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //4.返回数据
        return AppResult.success(sas);

    }



    //根据用户id查询scl90测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAll")
    public AppResult<List<SAS>> selectByUserId(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<SAS> sasList = iSasService.selectByUserId(stuUser.getId());

        //3.返回数据
        return AppResult.success(sasList);

    }

    //根据用户id查询scl90测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAllCon")
    public AppResult<List<SAS>> selectByUserIdCon(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED);
        }

        //2.调用service
        List<SAS> sasList = iSasService.selectByUserId(id);

        //3.返回数据
        return AppResult.success(sasList);

    }












}
